package org.apache.zookeeper.server.quorum;

import org.apache.zookeeper.server.DatadirCleanupManager;
import org.apache.zookeeper.server.QuorumPeer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.sasl.SaslException;

public class QuorumPeerMain {
    private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerMain.class);

    private static final String USAGE = "Usage: QuorumPeerMain configfile";
    protected QuorumPeer quorumPeer;

    public static void main(String[] args) {
        QuorumPeerMain main = new QuorumPeerMain();
        main.initializeAndRun(args);
    }

    private void initializeAndRun(String[] args) {
        QuorumPeerConfig config = new QuorumPeerConfig();
        if (args.length==1){
            // 设置解析步骤
            config.parse(args[0]);
        }
        // 启动并计划清除任务
        DatadirCleanupManager purgeMgr = new DatadirCleanupManager(
                config.getDataDir(),
                config.getDataLogDir(),
                config.getSnapRetainCount(),
                config.getPurgeInterval());
        purgeMgr.start();
        if (args.length==1 && config.isDistributed()){
            runFromConfig(config);
        }else {
            LOG.warn("Either no config or no quorum defined in config, running in standalone mode");
            // there is only server in the quorum -- run as standalone
//            ZooKeeperServerMain.main(args);
        }
    }

    private void runFromConfig(QuorumPeerConfig config) {

    }
    // @VisibleForTesting
    protected QuorumPeer getQuorumPeer() throws SaslException {
        return new QuorumPeer();
    }
}
